import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import 'font-awesome/css/font-awesome.css'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

import {postRequest} from "./utils/api";
import {putRequest} from "./utils/api";
import {getRequest} from "./utils/api";
import {deleteRequest} from "./utils/api";
import {initMenu} from "./utils/menus";

Vue.config.productionTip = false
Vue.use(ElementUI);
//插件形式使用请求
Vue.prototype.postRequest = postRequest;
Vue.prototype.putRequest = putRequest;
Vue.prototype.getRequest = getRequest;
Vue.prototype.deleteRequest = deleteRequest;

router.beforeEach((to, from, next) => {
  if (window.sessionStorage.getItem("tokenStr")) {
    initMenu(router, store);
    if (!window.sessionStorage.getItem("tokenUser") ||!window.sessionStorage.getItem("tokenCust")) {
      return getRequest('/getUserInfo').then(res => {
        if (res) {
          //存入用户信息
          window.sessionStorage.setItem("tokenCust", JSON.stringify(res.data.user));
          window.sessionStorage.setItem("tokenUser", JSON.stringify(res.data.user))
          next();
        }
      });
    }
    next();
  } else {
      next();
  }
})

      new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
